package com.EVO.dao;

import com.EVO.entity.Users;
import com.EVO.util.Util;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    public int add(Users user){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int result = 0;
        try {
            conn = Util.getConnection();
            ps = conn.prepareStatement("select userName from t_user where userName = ?");
            ps.setString(1,user.getUserName());
            rs = ps.executeQuery();
            if(!rs.next()){
                ps = conn.prepareStatement("insert into t_user(userName,password,sex,email) values(?,?,?,?)");
                ps.setString(1,user.getUserName());
                ps.setString(2,user.getPassword());
                ps.setString(3,user.getSex());
                ps.setString(4,user.getEmail());
                result = ps.executeUpdate();
                return result;
            }
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Util.close(conn,ps,rs);

        }
        return 0;
    }
    public int add(Users user, HttpServletRequest request){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int result = 0;
        try {
            conn = Util.getConnection(request);
            ps = conn.prepareStatement("select userName from t_user where userName = ?");
            ps.setString(1,user.getUserName());
            rs = ps.executeQuery();
            if(!rs.next()){
                ps = conn.prepareStatement("insert into t_user(userName,password,sex,email) values(?,?,?,?)");
                ps.setString(1,user.getUserName());
                ps.setString(2,user.getPassword());
                ps.setString(3,user.getSex());
                ps.setString(4,user.getEmail());
                result = ps.executeUpdate();
                return result;
            }
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Util.close(conn,ps,rs,request);

        }
        return 0;
    }
    public List findAll(){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List userList = new ArrayList();
        try {
            conn = Util.getConnection();
            ps = conn.prepareStatement("select * from t_user");
            rs = ps.executeQuery();
            while(rs.next()){
                Integer userId = rs.getInt("userId");
                String userName = rs.getString("userName");
                String password = rs.getString("password");
                String sex = rs.getString("sex");
                String email = rs.getString("email");
                Users users = new Users(userId,userName,password,sex,email);
                userList.add(users);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Util.close(conn,ps,rs);
        }


        return userList;
    }
    public int delete(String userId){
        Connection conn = null;
        PreparedStatement ps = null;
        int result = 0;
        try {
            conn = Util.getConnection();
            ps = conn.prepareCall("delete from t_user where userId = ?");
            ps.setString(1,userId);
            result = ps.executeUpdate();
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Util.close(conn,ps,null);
        }
        return result;
    }
    public int login(String userName,String password){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int result =0;
        try {
            conn = Util.getConnection();
            ps = conn.prepareStatement("select count(*) from t_user where userName = ? and password = ?");
            ps.setString(1,userName);
            ps.setString(2,password);
            rs = ps.executeQuery();
            while(rs.next()){
                result = rs.getInt("count(*)");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            Util.close(conn,ps,rs);
        }
        return result;
    }
}
